package com.glory.brepda.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.entity.EdcEntity;
import com.glory.brepda.entity.EdcSetBinEntity;
import com.glory.brepda.entity.LotInfoEntity;

import java.util.List;

public class ItmeDataSortAdapter extends BaseQuickAdapter<EdcEntity.EdcSet, BaseViewHolder> {

    public ItmeDataSortAdapter(@Nullable List<EdcEntity.EdcSet> data) {
        super(R.layout.item_itmedatasort_list);
    }

    @Override
    protected void convert(BaseViewHolder helper, EdcEntity.EdcSet item) {
        helper.setText(R.id.tvName,item.getITEMDESC());
        helper.setText(R.id.tvDescRiption,item.getDESCRIPTION());
        EditText etValue = helper.getView(R.id.etValue);
        EditText etComment = helper.getView(R.id.etComment);
        setCommentEditListener(etComment,item);
        setValueEditListener(etValue,item);

    }

    private void setCommentEditListener(EditText editText, EdcEntity.EdcSet item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setCOMMENT(s.toString());
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }

    private void setValueEditListener(EditText editText, EdcEntity.EdcSet item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String value = s.toString().equals("") ? "0" : s.toString();
                item.setVALUE(value);
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
